Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
postcss-values-parser
Advanced tools
The postcss-values-parser package is a tool for parsing CSS values, allowing developers to analyze and manipulate CSS strings in JavaScript. It provides a detailed AST (Abstract Syntax Tree) for CSS values, making it easier to understand and modify CSS properties programmatically.
Parsing CSS values
This feature allows you to parse any string representing CSS values into a structured AST. The code sample demonstrates parsing a simple CSS value string.
const parse = require('postcss-values-parser').parse;
let root = parse('10px solid black');
console.log(root.nodes);
Walking through the AST
After parsing CSS values into an AST, this feature enables walking through the AST nodes. This can be useful for analyzing or modifying specific parts of the CSS value.
const parse = require('postcss-values-parser').parse;
let root = parse('10px solid black');
root.walk((node) => console.log(node));
Modifying CSS values
This feature demonstrates how to modify CSS values after parsing. In the code sample, the size value of a CSS property is changed from '10px' to '15px'.
const parse = require('postcss-values-parser').parse;
let root = parse('10px solid black');
root.nodes[0].value = '15px';
console.log(root.toString());
css-tree is a CSS parser that produces an AST for CSS documents. It offers similar functionality for parsing CSS but is more focused on entire stylesheets rather than individual values. Compared to postcss-values-parser, css-tree provides a broader scope of CSS parsing capabilities.
css-what is a package for parsing CSS selectors. It provides functionality to parse selector strings into an understandable format but does not focus on CSS properties or values. While it offers parsing capabilities, its focus is different from that of postcss-values-parser, which is more centered on CSS values.
A CSS property value parser for use with PostCSS, following the same node, container, and traversal patterns as PostCSS.
:rocket: Are you ready to tackle ES6 and hone your JavaScript Skills? :rocket:
Check out these outstanding ES6 courses by @wesbos
As with PostCSS and postcss-selector-parser, this parser generates an Abstract Syntax Tree, (aka "AST") which allows for ease of traversal and granular inspection of each part of a property's value.
Yeah, it's a tad confusing. The Lesshint project needed a parser that would allow detailed inspection of property values to the same degree that PostCSS and postcss-selector-parser provided. This was especailly important for the Lesshint project, as it provides for very granular rules for linting LESS.
postcss-value-parser makes a lot of assumption about how values should be parsed and how the resulting AST should be organized. It was also fairly out of sync with the tokenzing and traversal patterns and convenience methods found in PostCSS and postcss-selector-parser.
So we needed an alternative, and drew upon all three projects to put together a value parser that met and exceeded our needs. The improvements include:
Please see the API Documentation for full usage information.
As with any NPM module, start with the install:
npm install postcss-values-parser
Using this parser is straightforward and doesn't require callbacks:
const parser = require('postcss-values-parser');
const ast = parser('#fff').parse();
let color = ast // the Root node
.first // the Value node
.first; // a Word node, containing the color value.
Loose mode was introduced to support adherence to the W3C CSS Specification as well as the ability to parse noncompliant CSS for variants like LESS, SCSS, and CSSNext. If you're working with a noncompliant or CSS-like variant, then loose mode is for you.
For example, the parser
will throw an error by default if calc
parameters don't adhere to the spec.
However, with loose mode enabled, the parse will ignore spec rules and succeed.
In-draft features, or CSS features in modules not yet finalized, often cause parser
errors. eg. url(var(--somevar))
. Loose mode supports parsing of these features.
Loose Mode is enabled by passing an option of loose: true
to the parser
method.
const less = 'calc(2+2)'; // not valid per spec, but valid in LESS
const cssnext = 'url(var(--somevar))'; // not valid per spec, but in spec draft
const parser = require('postcss-values-parser');
const ast = parser(less, { loose: true }).parse();
// parse will succeed
This project was heavily influenced by postcss-selector-parser and utilized many patterns and logical constructs from the project.
Tests and some tokenizing techniques found in postcss-value-parser were used.
git fork/clone
npm i
npm test
still pass. Add test if you're adding features.When you tweak API.md, please run npm run toc
before PR'ing.
FAQs
A CSS property value parser for use with PostCSS
The npm package postcss-values-parser receives a total of 2,988,739 weekly downloads. As such, postcss-values-parser popularity was classified as popular.
We found that postcss-values-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.